/**
 * a class representing a Quadrangle in two dimentional space.
 * Quadrangle is a polygon made of 4 corners (4 straight lines).
 * the Quadrangle represented is a CURVED quadrangle (his diagonals are inside the quadrangle area)
 * @author Matan Monitz, Nadav Cohen
 *
 */
public class Quadrangle extends Polygon {
	
	/**
	 * a constructor for quadrangle by using 4 instances of Point for corners.
	 * the 4 corners will be ordered to represent a single closed Quadrangle - first point to second, second to third, third to forth, forth to first
	 * the 4 corners will be different from each other.
	 * the 4 corners will not be on the same line (will represent a valid quadrangle and combination of 3 of the points will not represent a one dimenational line)   
	 * @param p1 an instance of Point. must be initialized 
	 * @param p2 an instance of Point. must be initialized
	 * @param p3 an instance of Point. must be initialized
	 * @param p4 an instance of Point. must be initialized
	 */
	public Quadrangle(Point p1, Point p2, Point p3, Point p4)
	{
		super(new Point[] {p1,p2,p3,p4});
	}
	
	/**
	 * a copy constructor for Quadrangle utilizing deep copy semantics
	 * @param t an instance of Quadrangle. must be initialized and represent a valid Quadrangle
	 */
	public Quadrangle(Quadrangle q) 
	{
		super(new Point[] {q.getP1(),q.getP2(),q.getP3(),q.getP4()});
	}
	
	/**
	 * a getter for the first corner of the Quadrangle
	 * @return a new instance of Point equal to the first corner of the Quadrangle
	 */
	public Point getP1()
	{
		Point temppoints[] = super.getPoints();
		return new Point(temppoints[0]);
	}
	
	/**
	 * a getter for the second corner of the Quadrangle
	 * @return a new instance of Point equal to the second corner of the Quadrangle
	 */
	public Point getP2()
	{
		Point temppoints[] = super.getPoints();
		return new Point(temppoints[1]);
	}
	
	/**
	 * a getter for the third corner of the Quadrangle
	 * @return a new instance of Point equal to the third corner of the Quadrangle
	 */
	public Point getP3()
	{
		Point temppoints[] = super.getPoints();
		return new Point(temppoints[2]);
	}
	
	/**
	 * a getter for the forth corner of the Quadrangle
	 * @return a new instance of Point equal to the forth corner of the Quadrangle
	 */
	public Point getP4()
	{
		Point temppoints[] = super.getPoints();
		return new Point(temppoints[3]);
	}
	
	
	public boolean equals(Object other)
	{
		if(other instanceof Quadrangle)
			return super.equals(other);
		else
			return false;
	}

	/**
	 * get the area of the quadrangle.
	 * calculate by seperating the quad by one of its diagonal into 2 triangles. calculate each triangle area, and sum the results.
	 * @return double representing the area inside the shape
	 */
	public double getArea()
	{
		Triangle l,r;
		l=new Triangle(getP1(),getP2(),getP3());  //build a triangle from point 1 , 2 , 3 of quadrangle
		r=new Triangle(getP1(),getP4(),getP3());  //build a triangle from point 1 , 4 , 3 of quadrangle
		return l.getArea()+r.getArea(); // sum of 2 triangle area == quad area 
	}
	
	/**
	 * checks if a given point is located inside the area of the quadrangle.
	 * checks by seperating the quad by one of its diagonal into 2 triangles, then check if point exist in first OR second triangle.
	 * @param p - an instance of Point to be checked. must be initialized.
	 * @exception RuntimeException in case given parameter uninitialized - "Point sent uninitialized"
	 * @return true if the point is inside the quadrangle. false if the point is outside the quadrangle. 
	 */	
	public boolean contains(Point p)
	{
		if(p==null)
			throw new RuntimeException("error in quadrangle contains check - Point sent uninitialized!");
		Triangle l,r;
		l=new Triangle(getP1(),getP2(),getP3());  //build a triangle from point 1 , 2 , 3 of quadrangle
		r=new Triangle(getP1(),getP4(),getP3());  //build a triangle from point 1 , 4 , 3 of quadrangle
		return (l.contains(p) || r.contains(p));
	}
}
